package jhh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jhh.entity.Jmx;
import jhh.entity.Jtl;
import jhh.service.Ant_JmeterImp;
import jhh.service.Delete;
import jhh.service.JtlReader;
import jhh.service.NewJmx;
import jhh.service.iface.Jmeter;
@WebServlet("/LoadTest")
public class LoadTest extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String JmxPath=this.getServletContext().getRealPath("upload");
		String JtlPath=this.getServletContext().getRealPath("result");
		Delete del=new Delete();
		del.deleteAll(JmxPath);
		del.deleteAll(JtlPath);
		String url=request.getParameter("URL");
		//url="www.baidu.com";
		Jmx jmx=new Jmx();
		jmx.setSample_error("continue");
		jmx.setHTTPSampler_path("");
		jmx.setHTTPSampler_method("GET");
		jmx.setHTTPSampler_port(80);
		jmx.setLoop(1);
		int[] num=new int[]{1,2,3,4,5,6,7,8,9,10};
		for(int i=0;i<10;i++){
			String s="LoadTest"+i;
			int k=num[i];
			jmx.setHTTPSamplerProxy("Test"+i);
			jmx.setTestplan(s);
			jmx.setThreadGroup(s);
			jmx.setNum_threads(k);
			jmx.setHTTPSampler_domain(url);
			 String TemPath=this.getServletContext().getRealPath("WEB-INF/Temple.xml");
	          String Path=this.getServletContext().getRealPath("upload");
	          NewJmx newJmx=new NewJmx();
	          newJmx.createJmx(jmx, TemPath, Path);
			
			
		}
		String path1=this.getServletContext().getRealPath("WEB-INF/build.xml");
		Jmeter a=new Ant_JmeterImp();
		a.DoJmx(path1);
		String Jtlpath=this.getServletContext().getRealPath("result/jmeter_result.jtl");
		JtlReader reader=new JtlReader();
		List<Jtl> list=reader.JtlReader(Jtlpath);
		int []sum=new int[10];
		int []count=new int[10];
		int i=0;
	    for (Jtl jtl : list) {
	         if(jtl.getTn().contains("LoadTest0")){
	        	 int k=jtl.getT();
	        	 sum[0]+=k;
	        	 count[0]++;
	         }else if(jtl.getTn().contains("LoadTest1")){
	        	 int k=jtl.getT();
	        	 sum[1]+=k;
	        	 count[1]++;
	         }else if(jtl.getTn().contains("LoadTest2")){
	        	 int k=jtl.getT();
	        	 sum[2]+=k;
	        	 count[2]++;
	         }else if(jtl.getTn().contains("LoadTest3")){
	        	 int k=jtl.getT();
	        	 sum[3]+=k;
	        	 count[3]++;
	         }else if(jtl.getTn().contains("LoadTest4")){
	        	 int k=jtl.getT();
	        	 sum[4]+=k;
	        	 count[4]++;
	         }else if(jtl.getTn().contains("LoadTest5")){
	        	 int k=jtl.getT();
	        	 sum[5]+=k;
	        	 count[5]++;
	         }else if(jtl.getTn().contains("LoadTest6")){
	        	 int k=jtl.getT();
	        	 sum[6]+=k;
	        	 count[6]++;
	         }else if(jtl.getTn().contains("LoadTest7")){
	        	 int k=jtl.getT();
	        	 sum[7]+=k;
	        	 count[7]++;
	         }else if(jtl.getTn().contains("LoadTest8")){
	        	 int k=jtl.getT();
	        	 sum[8]+=k;
	        	 count[8]++;
	         }else if(jtl.getTn().contains("LoadTest9")){
	        	 int k=jtl.getT();
	        	 sum[9]+=k;
	        	 count[9]++;
	         }
		}
	    HttpSession session = request.getSession();		
	    session.setAttribute("sum",sum);
	    session.setAttribute("count", count);
		response.sendRedirect("LineChart");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
